package org.yang.snake.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.yang.snake.model.SysMenu;
import org.yang.snake.service.SysMenuService;
import org.yang.snake.util.SecurityUtil;
import org.yang.snake.util.TreeUtil;
import org.yang.snake.vo.MenuTreeVO;

import java.util.ArrayList;
import java.util.List;

/**
 * 登录控制器
 * @author shichenyang
 */
@Controller
public class LoginController {
    @Autowired
    private SysMenuService sysMenuService;
    @GetMapping("/")
    public String index(Model model){
        SnakeUser user= SecurityUtil.getUser();
        SysMenu menu=new SysMenu();
        menu.setMenu(1);
        menu.setId(user.getId());
        List<SysMenu> resources= sysMenuService.listIndexMenu(menu);
        List<MenuTreeVO> menuList;
        List<MenuTreeVO> finalMenuList = new ArrayList<>();
        resources.forEach(item -> {
            MenuTreeVO vo=new MenuTreeVO(item.getId(),item.getParentId(),item.getUrl(),item.getIcon(),item.getName(),null);
            finalMenuList.add(vo);
        });
        menuList= TreeUtil.buildByLoop(finalMenuList,0L);
        model.addAttribute("menu",menuList);
        model.addAttribute("username",user.getUsername());
        return "index";
    }
    @GetMapping("/login.html")
    public String login(){
        return "login";
    }
}
